package com.google.codejam.core.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * Reader used for reading test cases from the input. This object makes
 * use of a <code>BufferedReader</code> internally.
 */
public class TestCaseInputReader
{
	private final BufferedReader buffered_reader;

	public TestCaseInputReader(InputStream in)
	{
		buffered_reader =
				  new BufferedReader(new InputStreamReader(in));
	}

	public TestCaseInputReader(
			  InputStream in,
			  Charset charset)
	{
		buffered_reader =
				  new BufferedReader(new InputStreamReader(in, charset));
	}

	public TestCaseInputReader(
			  InputStream in,
			  int buffer_size)
	{
		buffered_reader =
				  new BufferedReader(new InputStreamReader(in), buffer_size);
	}

	public TestCaseInputReader(
			  InputStream in,
			  Charset charset,
			  int buffer_size)
	{
		buffered_reader =
				  new BufferedReader(new InputStreamReader(in, charset),
				                     buffer_size);
	}

	public final String readLine()
			  throws IOException
	{
		return buffered_reader.readLine();
	}

	public final BufferedReader getReader()
	{
		return buffered_reader;
	}

	public final void close()
			  throws IOException
	{
		if (buffered_reader != null)
		{
			buffered_reader.close();
		}
	}

	public void finalize()
			  throws Throwable
	{
		if (buffered_reader != null)
		{
			buffered_reader.close();
		}

		super.finalize();
	}
}
